<template>
  <!-- 菜单管理 -->
  <div>
    <div>
      <h1>菜单管理</h1>
    </div>
    <el-form :inline="true">
      <el-row>
        <el-col :span="6">
          <el-form-item label="菜单名称：">
            <el-input
              v-model="selectData.menuName"
              placeholder="请输入菜单名称"
              style="width: 240px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="请选择状态：">
            <el-select
              v-model="selectData.menuStatus"
              @change="handleStatusChange"
              style="width: 200px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="getMenuHandler">查询</el-button>
          <el-button type="primary" @click="resetHandler">重置</el-button>
          <el-button type="primary" @click="openAddDialog">添加菜单</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 表格数据-->
    <el-table :data="tableData" border style="width: 100%; margin-top: 15px" v-loading="dataLoading" element-loading-text="拼命加载中">
      <el-table-column prop="menuId" label="菜单ID"> </el-table-column>
      <el-table-column prop="menuName" label="菜单名称"> </el-table-column>
      <el-table-column prop="parentId" label="菜单所属父级ID">
      </el-table-column>
      <el-table-column prop="menuStatus" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.menuStatus == 0" type="success">启用</el-tag>
          <el-tag v-else-if="scope.row.menuStatus == 1" type="danger"
            >禁用</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="openUpdateDialog(scope.row)"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.menuStatus == 0"
            size="mini"
            type="danger"
            @click="updateMenuStatusHandler(scope.row)"
            >禁用</el-button
          >
          <el-button
            v-if="scope.row.menuStatus == 1"
            size="mini"
            type="primary"
            @click="updateMenuStatusHandler(scope.row)"
            >启用</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增弹窗 -->
    <el-dialog title="新增菜单" width="30%" :visible.sync="addDialogVisible">
      <el-form>
        <el-form-item label="菜单名称：">
          <el-input v-model="addData.menuName"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径：">
          <el-input v-model="addData.menuPath"></el-input>
        </el-form-item>
        <el-form-item label="是否为顶级父菜单">
          <el-radio-group v-model="addData.isParent" @change="setAddParentId">
            <el-radio :label="false">是</el-radio>
            <el-radio :label="true">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="请选择父级菜单" v-if="addData.isParent">
          <el-select v-model="addData.parentId" style="width: 200px">
            <el-option
              v-for="item in optionList"
              :key="item.menuId"
              :label="item.menuName"
              :value="item.menuId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addMenuHandler">提交</el-button>
        <el-button @click="cancelAddMenuHanlder">取消</el-button>
      </div>
    </el-dialog>

    <!-- 修改弹窗 -->
    <el-dialog
      title="修改菜单信息"
      width="30%"
      :visible.sync="updateDialogVisible"
    >
      <el-form>
        <el-form-item label="菜单名称：">
          <el-input v-model="updateData.menuName"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径：">
          <el-input v-model="updateData.menuPath"></el-input>
        </el-form-item>
        <el-form-item label="是否修改菜单层级">
          <el-radio-group
            v-model="updateData.isParent"
            @change="setUpdateParentId"
          >
            <el-radio :label="false">否</el-radio>
            <el-radio :label="true">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="请选择父级菜单" v-if="updateData.isParent">
          <el-select
            v-model="updateData.parentId"
            style="width: 200px"
            :placeholder="`请选择`"
          >
            <el-option :key="0" :label="`[顶级菜单]`" :value="0"></el-option>
            <template v-for="item in optionList">
              <el-option
                v-if="item.menuId !== updateData.menuId"
                :key="item.menuId"
                :label="item.menuName"
                :value="item.menuId"
              >
              </el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateMenuHandler">提交</el-button>
        <el-button @click="cancelUpdateMenuHanlder">取消</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="selectData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="selectData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getMenu, getMenuId, addMenu, updateMenu } from "@/api/menu";
export default {
  data() {
    return {
      //加载状态
      dataLoading:false,
      parentPlaceholder: "",
      addDialogVisible: false,
      updateDialogVisible: false,
      options: [
        { value: "", label: "全部" },
        { value: "0", label: "启用" },
        { value: "1", label: "禁用" },
      ],
      tableData: [],
      optionList: [],
      selectData: {
        //暂时先赋予超级管理员ID
        adminId: this.$store.state.adminId,
        menuName: null,
        menuStatus: "",
        currentPage: 1, //当前页
        pageSize: 5, //每页大小
      },
      total: 0,
      addData: {
        isParent: null,
        menuName: null,
        menuPath: null,
        parentId: null,
      },
      updateData: {
        isParent: null,
        menuId: null,
        menuName: null,
        menuPath: null,
        parentId: null,
        originalParentId: null,
        menuStatus: "",
      },
    };
  },
  methods: {
    //修改菜单状态
    updateMenuStatusHandler(row) {
      this.$confirm(
        `此操作将修改菜单名为${row.menuName}的状态, 是否继续?`,
        "温馨提示",
        {
          confirmButtonText: "确定修改",
          cancelButtonText: "取消修改",
        }
      )
        .then(() => {
          const statusData = {
            menuId: row.menuId,
            menuStatus: row.menuStatus === 0 ? 1 : 0,
          };
          updateMenu(statusData).then((res) => {
            this.updateDialogVisible = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.getMenuHandler();
            }
          });
        })
        .catch(() => {});
    },
    //取消修改行业按钮
    cancelUpdateMenuHanlder() {
      this.updateDialogVisible = false;
    },
    //提交修改菜单
    updateMenuHandler() {
      this.$confirm(
        `此操作将修改菜单名为${this.updateData.menuName}的菜单信息, 是否继续?`,
        "温馨提示",
        {
          confirmButtonText: "确定修改",
          cancelButtonText: "取消修改",
        }
      )
        .then(() => {
          updateMenu(this.updateData).then((res) => {
            this.updateDialogVisible = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.getMenuHandler();
            }
          });
        })
        .catch(() => {});
    },

    //修改菜单层级关系选择无父级菜单时设置为父级菜单
    setUpdateParentId() {
      if (!this.updateData.isParent) {
        this.updateData.parentId = 0;
      }
      this.updateData.parentId = this.updateData.originalParentId;
    },

    //打开修改弹窗的方法
    openUpdateDialog(row) {
      this.updateData.menuId = row.menuId;
      this.updateData.menuName = row.menuName;
      this.updateData.menuPath = row.menuPath;
      this.updateData.parentId = row.parentId;
      this.updateData.originalParentId = row.parentId;
      this.updateData.isParent = row.parentId !== 0;
      this.updateDialogVisible = true;
      this.getMenuOptionHandler();
    },
    //取消新增行业按钮
    cancelAddMenuHanlder() {
      this.addDialogVisible = false;
    },
    //新增菜单
    addMenuHandler() {
      this.$confirm(`此操作将新增菜单, 是否继续?`, "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          addMenu(this.addData).then((res) => {
            this.addDialogVisible = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "新增成功",
              });
              this.getMenuHandler();
            }
          });
        })
        .catch(() => {});
    },
    //无父菜单的情况
    setAddParentId() {
      if (!this.addData.isParent) {
        this.addData.parentId = 0;
      }
      this.addData.parentId = null;
    },
    //打开新增弹窗的方法
    openAddDialog() {
      this.addDialogVisible = true;
      this.addData.menuName = null;
      this.addData.menuPath = null;
      this.addData.isParent = null;
      this.addData.parentId = null;
      this.getMenuOptionHandler();
    },
    //重置
    resetHandler() {
      this.selectData.menuName = null;
      this.selectData.menuStatus = "";
      this.getMenuHandler();
    },
    //查询不同状态的菜单
    handleStatusChange() {
      this.selectData.currentPage = 1;
    },
    //查询
    getMenuHandler() {
      //开启加载动画
      this.dataLoading=true;
      getMenu(this.selectData)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.total;
          this.selectData.currentPage = res.data.pageNum
          //关闭加载动画
          this.dataLoading=false;
        })
        .catch(() => {});
    },

    //查询菜单Id
    getMenuOptionHandler() {
      getMenuId()
        .then((res) => {
          this.optionList = res.data;
        })
        .catch(() => {});
    },

    handleSizeChange(val) {
      this.selectData.pageSize = val;
      this.getMenuHandler();
    },
    handleCurrentChange(val) {
      this.selectData.currentPage = val;
      this.getMenuHandler();
    },
  },
  mounted() {
    this.getMenuHandler();
  },
};
</script>

<style>
</style>
